10. Classes and Objects

10 Classes And Objects

1- Copy this code to your editor and save it on your VM Desktop as Robotic.cpp:

#include <iostream>
using namespace std;

class Robot
{ 
  public:
    int Speed()
    {
    return 10;        
    }
};

int main() 
{

  Robot robot1;
  cout << "Speed= " << robot1.Speed() << endl;  

  return 0;
}                                                               

2- Next, Navigate to Desktop:

$ cd Desktop

3- It’s time to compile the code:

$ g++ Robotic.cpp -o app

4- Now, run your program:

$ ./app

5- After running the executable file, check the robot speed displayed in your terminal:

Class and Object Syntax

How would you instantiate an “apple” object from a “Fruit” class?

SOLUTION: Fruit apple;

Access Modifiers

What is the output of this code?

#include <iostream>
using namespace std;

class Human
{ 
  private:
    int age()
    {
    return 24;        
    }
};

int main() 
{

  Human Rony;
  cout << "Rony's age = " << Rony.age() << endl;  

  return 0;
}                                                               
SOLUTION: Error